<?php
	/**
	* ABSoft Banner Module
	* List Banners in many ways: 
	*   - text only: Just show a text
	*   - Images banner: show image as banner
	*   - Flash banner
	* Support show type:
	*  - Normal: show first item for selected type
	*  - Rotator: Roate each items in list of selected type
	* 
	* @author: trungdt
	* @since: Apr 2, 2011
	* @version: 1.0
	* 
	* DB Structure
	* CREATE TABLE IF NOT EXISTS `ag_abbannermodule_config` (
	`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
	`location_data` varchar( 250 ) COLLATE utf8_unicode_ci NOT NULL ,
	PRIMARY KEY ( `id` )
	) ENGINE = MYISAM DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT =1;

	ALTER TABLE `ag_abbannermodule_config` ADD `show_type` INT( 1 ) NOT NULL DEFAULT '0' COMMENT 'Module Show type; =0: text show, =1: image, ; =2: flash',
	ADD `mwidth` INT NOT NULL DEFAULT '0' COMMENT 'Module width',
	ADD `mheight` INT NOT NULL DEFAULT '0' COMMENT 'Module height',
	ADD `rotator` INT( 1 ) NOT NULL DEFAULT '0' COMMENT '=0: No roate, show first item, =1: Show random item in list;'  

	CREATE TABLE `ag`.`ag_abbanner` (
	`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	`location_data` VARCHAR( 250 ) NOT NULL
	) ENGINE = MYISAM ;

	ALTER TABLE `ag_abbanner` CHANGE `location_data` `location_data` VARCHAR( 250 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL 

	ALTER TABLE `ag_abbanner` ADD `show_type` INT( 1 ) NOT NULL DEFAULT '0' COMMENT 'Compare with show type in config'

	ALTER TABLE `ag_abbanner` ADD `rank` INT( 8 ) NOT NULL DEFAULT '0'

	ALTER TABLE `ag_abbanner` ADD `text_value` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL 

	ALTER TABLE `ag_abbanner` ADD `img_fileid` INT( 11 ) NOT NULL DEFAULT '0',
	ADD `img_title` TEXT NOT NULL ,
	ADD `img_linkurl` TEXT NOT NULL 

	ALTER TABLE `ag_abbanner` CHANGE `img_title` `img_title` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
	CHANGE `img_linkurl` `img_linkurl` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL 

	ALTER TABLE `ag_abbanner` ADD `flash_fileid` INT( 11 ) NOT NULL DEFAULT '0',
	ADD `flash_linkurl` TEXT NOT NULL 


	* 
	* Change logs
	* Apr 20, 2011 - trungdt - Add two banners image show type
	* Aur 23, 2011 - trungdt - add show type on homepage for Thanhoc online
	*/

	class abbannermodule {
		function name() { return "ABSoft Banners Module"; }
		function description() { return "Manages set of banners, support many banner type"; }
		function author() { return "ABSoft Vietnam"; }

		function hasSources() { return true; }
		function hasContent() { return true; }
		function hasViews() { return true; }

		function supportsWorkflow() { return false; }

		function permissions($internal = "") {
			return array(
			'administrate'=>'Administrate',
			'configure'=>'Configure',
			'create'=>'Add banner',
			'edit'=>'Edit banner',
			'delete'=>'Delete banner'
			);
		}

		function show($view,$loc = null, $title = "") {
			global $db, $browser;

			$config = $db->selectObject('abbannermodule_config',"location_data='".serialize($loc)."'");
			if ($config == null) {
				$template = new template('abbannermodule','notconfigured',$loc);
        $template->output();
			}
			else
			{
				$template = new template('abbannermodule',$view,$loc);
				// if this user has permission to manage
				if (!$browser->isMobile() && exponent_users_isLoggedIn() && (
				exponent_users_isAdmin() ||
				exponent_permissions_check("administrate",$loc) ||
				exponent_permissions_check("configure",$loc) ||
				exponent_permissions_check("create",$loc) ||
				exponent_permissions_check("edit",$loc) ||
				exponent_permissions_check("delete",$loc)   
				))
				{
					$template->assign("in_admin_mode",1);
					// dont show anything, all manage items must be done in Manage Action
				}
				else
				{
					$template->assign("in_admin_mode",0);
					// determine what view we will load
					$where=" AND show_type = {$config->show_type} ";
					if ($config->rotator == 0 ) // first item
					{
						$where.=" ORDER BY rank ASC LIMIT 0,1";
					}
					elseif ($config->rotator == 1) // // Last item
					{
						$where.=" ORDER BY rank DESC LIMIT 0,1";
					}
					elseif ($config->rotator == 2) // random
					{
						$where.=" ORDER BY rand() LIMIT 0,1";
					}
					elseif ($config->rotator == 3) // slide from first to last, only valid for image only
					{
						if ($config->show_type == 1 )
							$where.=" ORDER BY rank ASC";
						else
						{
							$where.= " LIMIT 0,1";
						}
					}
					elseif ($config->rotator == 4) // slide from last to first, only valid for image only
					{
						if ($config->show_type == 1 )
							$where.=" ORDER BY rank DESC";
						else
						{
							$where.= " LIMIT 0,1";
						}
					}
					elseif ($config->rotator == 5) // slide random, only valid for image only
					{
						if ($config->show_type == 1 )
							$where.=" ORDER BY rand() ";
						else
						{
							$where.= " LIMIT 0,1";
						}
					}
					elseif ($config->rotator == 6) // two image banners type
					{
						 if ($config->show_type == 1 )
							$where.=" ORDER BY rank DESC LIMIT 0,2 ";
						else
						{
							$where.= " LIMIT 0,1";
						}
						$template->assign("rotator_6_width",floor($config->mwidth/2)-3);
						$template->assign("rotator_6_height",floor($config->mheight/2));
					}

						// just show this module as it configured
					$link = $db->selectObjects('abbanner',"location_data='".serialize($loc)."' {$where} ");

					if (count($link))
					{
						foreach ($link as $key=>$value)
						{
							if ($value->show_type == 1)
							{
								$file = $db->selectObject('file', 'id='.$value->img_fileid);
								$value->img_path= PATH_RELATIVE.$file->directory.'/'.$file->filename;
							}
							else if ($value->show_type == 2){
									$file = $db->selectObject('file', 'id='.$value->flash_fileid);
									$value->flash_path= PATH_RELATIVE.$file->directory.'/'.$file->filename;
								}    
						}
						$temp=null;
						if (!(($config->show_type == 1) && ($config->rotator >=3)))
						{
							foreach ($link as $key=>$value)
							{
								$temp=$value;
								break;
							}
						}
						else
						{
							$temp=$link;
						}
						$template->assign("item",$temp);
					}   
					else
					{
						$template->assign("item",0);
					}
				}

				// feb 11, 2011 - trungdt - end
                global $browser;
                if ($browser->getBrowser() == Browser::BROWSER_OPERA)
                    $config->mwidth-=8;
               
                 
				$template->assign("config",$config);
				$template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
				$template->output();
			}


		}

		function deleteIn($loc) {
			global $db;
			$db->delete('linklist_link',"location_data='".serialize($loc)."'");
		}

		function copyContent($oloc,$nloc) {
			foreach ($db->selectObjects('linklist_link',"location_data='".serialize($oloc)."'") as $l) {
				$l->location_data = serialize($nloc);
				$db->insertObject($l,'linklist_link');
			}
		}

		// no search support
		function searchName() {
			return false;
		}

		function spiderTemplate($item)
		{

		}
	}

?>
